
#include "msgDefine.h"
#include "GameDanger.h"
#include "BattleSession.h"
#include "BattleManager.h"
#include "MapManager.h"


RECV_MSG_BATTLE_RESULT gMsgBattleResult;

BattleSession::BattleSession()
:	mNumEnemy( 0 ) , mNumberPlayer( 0 ) , mNumberCommand( 0 ) , mNumberCommandPVP( 0 ) ,
	mType( SABST_COUNT ) , 
	mTurn( 0 ) , mNpc( F_NULL )
{
	for ( fint32 i = 0 ; i < MAX_BATTLE_PLAYER ; ++i )
	{
		mPlayer[ i ] = F_NULL;
		mPlayerPVP[ i ] = F_NULL;
		
		mWaitCommand[ i ] = F_TRUE;
		mWaitCommandPVP[ i ] = F_TRUE;
	}

	mVectorAttack.reserve( MAX_BATTLE_ENEMY * 2 );
}


BattleSession::~BattleSession()
{

}


fvoid				BattleSession::InitPVP( GamePlayer* player1 , GamePlayer* player2 )
{
	mType = SABST_PVP;
	mPlayer[ 0 ] = player1;
	mNumberPlayer = 1;

	mPlayerPVP[ 0 ] = player2;
	mNumberPlayerPVP = 1;

	GameTeam* team = player1->GetTeam();
	if ( team )
	{
		if ( !team->IsLeader( player1 ) )
		{
			assert( 0 );
			return;
		}

		for ( fint32 i = 1 ; i < team->mMemberCount ; i++ )
		{
			mPlayer[ i ] = (GamePlayer*)team->mMembers[ i ];
		}

		mNumberPlayer = team->mMemberCount;
	}

	team = player2->GetTeam();
	if ( team )
	{
		if ( !team->IsLeader( player2 ) )
		{
			assert( 0 );
			return;
		}

		for ( fint32 i = 1 ; i < team->mMemberCount ; i++ )
		{
			mPlayerPVP[ i ] = (GamePlayer*)team->mMembers[ i ];
		}

		mNumberPlayerPVP = team->mMemberCount;
	}

}


fvoid				BattleSession::InitPVE( GamePlayer* player , fint32 num , fint32 petNum , GameNpc* npc /* = F_NULL */ )
{
	mType = SABST_PVE;
	mPlayer[ 0 ] = player;
	mNumberPlayer = 1;
	mNumberPlayerPVP = 0;
	mNumEnemy = num;
	mNumEnemyPet = petNum;
	mNpc = npc;

	GameTeam* team = player->GetTeam();

	if ( team )
	{
		if ( !team->IsLeader( player ) )
		{
			assert( 0 );
			return;
		}

		for ( fint32 i = 1 ; i < team->mMemberCount ; i++ )
		{
			mPlayer[ i ] = (GamePlayer*)team->mMembers[ i ];
		}

		mNumberPlayer = team->mMemberCount;
	}
}


GameEnemyEntity*	BattleSession::GetEnemy( fint32 index )
{
	return mEnemyEntity + index;
}


GameEnemyEntity*	BattleSession::GetEnemyPet( fint32 index )
{
	return mEnemyEntityPet + index;
}


fvoid				BattleSession::Release()
{
	for ( fint32 i = 0 ; i < mNumberPlayer ; ++i )
	{
		mPlayer[ i ]->EndFight();
	}

	for ( fint32 i = 0 ; i < mNumberPlayerPVP ; ++i )
	{
		mPlayerPVP[ i ]->EndFight();
	}

	for ( fint32 i = 0 ; i < MAX_BATTLE_PLAYER ; ++i )
	{
		mWaitCommand[ i ] = F_TRUE;
		mWaitCommandPVP[ i ] = F_TRUE;
		
		mPlayer[ i ] = F_NULL;
		mPlayerPVP[ i ] = F_NULL;
	}

	if ( mNpc )
	{
//		NpcData& data = mNpc->GetNpcData();
//		if ( data.DieAction == SANDM_DIE )
//		{
//			mNpc->SetFall( F_TRUE );
//			
//			MapScene* scene = gMapManager->GetScene( mNpc->mMoveData.Pos.MapID );
//			scene->MoveNpc( mNpc );
//			
//			gMapManager->AddWaitNpc( mNpc );
//		}
//		else
//		{
//			MapPos pos;
//			pos.Grid.x = data.WarpX;
//			pos.Grid.y = data.WarpY;
//			pos.MapID = data.WarpID;
//
//			if ( data.WarpX && data.WarpY && data.WarpID )
//			{
//				gMapManager->WarpScene( mPlayer[0] , pos );
//			}
//		}
//		
//		mNpc = F_NULL;
	}
	
	mNpc = F_NULL;
	mType = SABST_COUNT;
	mTurn = 0;
	mNumEnemy = 0;
	mNumEnemyPet = 0;
	mNumberPlayer = 0;
	mNumberPlayerPVP = 0;
	mNumberCommand = 0;
	mNumberCommandPVP = 0;
}


fint16				BattleSession::Down( GameCreature* c1 , GameCreature* c2 )
{
	freal32 atk = c1->mFightData.Base.Attack;
	freal32 def = c2->mFightData.Base.Defence;

	if ( c2->mCommand.cmd.Type == SABSCT_DEFENCE )
	{
		def *= 1.5f;
	}
	
	freal32 attr = ( c1->mFightData.Base.Earth * c2->mFightData.Base.Water + c1->mFightData.Base.Water * c2->mFightData.Base.Fire + 
		c1->mFightData.Base.Fire * c2->mFightData.Base.Wind + c1->mFightData.Base.Wind * c2->mFightData.Base.Earth ) * 0.0001f;
	
	attr -= ( c2->mFightData.Base.Earth * c1->mFightData.Base.Water + c2->mFightData.Base.Water * c1->mFightData.Base.Fire + 
		c2->mFightData.Base.Fire * c1->mFightData.Base.Wind + c2->mFightData.Base.Wind * c1->mFightData.Base.Earth ) * 0.0001f;
	
	freal32 r = 0;//MathBase::GetRandomFloat( -0.15f , 0.15f );
	
	freal32 down = ( ( atk * ( 2.0f + attr ) ) - def * 1.5f ) * ( 1.0f + r );
	
	if ( down <= 0.0f )
	{
		down = 1.0f;
	}
	
	return (fint16)down;
}


fint16				BattleSession::ComputerFight( GameEnemyEntity* enemy )
{
	fint16 down = Down( enemy , mPlayer[0] );

	if ( down <= 0 )
	{
		down = 1;
	}

	return 0;
}


fvoid				BattleSession::ComputerRandCommand( GameEnemyEntity* enemy )
{
	// pve 

	BattleSessionCommand& command = enemy->mCommand;

	fint32 min = 0;
	fint32 max = enemy->mEnemy->intValue[ GEIV_ATTACK ] + enemy->mEnemy->intValue[ GEIV_DEFENCE ] + 
		enemy->mEnemy->intValue[ GEIV_MAGIC ] + enemy->mEnemy->intValue[ GEIV_ESCAPE ];

	fint32 rand = 0;//MathBase::GetRandomInt( min , max );

	if ( rand < enemy->mEnemy->intValue[ GEIV_ATTACK ] )
	{
		fint32 index = 0;//MathBase::GetRandomInt( 0 , mList.size() );
		GameCreatureListIter iter = mList.begin();
		for ( fint32 i = 0 ; i < index ; ++i )
		{
			++iter;
		}

		command.cmd.Type = SABSCT_ATTACK;
		command.cmd.TargetIndex = (*iter)->mCommand.Index;
		command.cmd.TargetRow = (*iter)->mCommand.Row;

		return;
	}

	rand -= enemy->mEnemy->intValue[ GEIV_ATTACK ];

	if ( rand < enemy->mEnemy->intValue[ GEIV_DEFENCE ] )
	{
		command.cmd.Type = SABSCT_DEFENCE;
		return;
	}

	rand -= enemy->mEnemy->intValue[ GEIV_DEFENCE ];

	if ( rand < enemy->mEnemy->intValue[ GEIV_MAGIC ] )
	{
		command.cmd.Type = SABSCT_MAGIC;
		return;
	}

	//command.Type = BSCT_ESCAPE;
	//command.cmd.TargetIndex = 0;
	command.cmd.Type = SABSCT_STAY;
}

fvoid				BattleSession::DoResult( GameCreatureVector& v )
{
	if ( v.empty() )
	{
		return;
	}

	GameCreature* c = v[ 0 ];

	switch ( c->mCommand.cmd.Type )
	{
	case SABSCT_ATTACK:
		{
			GameCreature* c = v[ 0 ];
			GameCreature* target = F_NULL;

			switch ( (fint32)mType )
			{
			case SABST_PVE:
				{
					if ( c->mCommand.Side == SABSS_SELF )
					{
						if ( c->mCommand.cmd.TargetRow == SABSR_UNDER )
						{
							if ( c->mCommand.cmd.TargetIndex < mNumEnemy )
								target = mEnemyEntity + c->mCommand.cmd.TargetIndex;
						}
						else
						{
							if ( c->mCommand.cmd.TargetIndex < mNumEnemyPet )
								target = mEnemyEntityPet + c->mCommand.cmd.TargetIndex;
						}
					}
					else
					{
						if ( c->mCommand.cmd.TargetRow == SABSR_UNDER )
						{
							if ( c->mCommand.cmd.TargetIndex < mNumberPlayer )
								target = mPlayer[ c->mCommand.cmd.TargetIndex ];
						}
						else
						{
							if ( c->mCommand.cmd.TargetIndex < mNumberPlayer )
								target = mPlayer[ c->mCommand.cmd.TargetIndex ]->GetAttackPet();
						}
					}
				}
                    break;
			case SABST_PVP:
				{
					if ( c->mCommand.Side == SABSS_SELF )
					{
						if ( c->mCommand.cmd.TargetRow == SABSR_UNDER )
						{
							if ( c->mCommand.cmd.TargetIndex < mNumberPlayerPVP )
								target = mPlayerPVP[ c->mCommand.cmd.TargetIndex ];
						}
						else
						{	if ( c->mCommand.cmd.TargetIndex < mNumberPlayerPVP )
								target = mPlayerPVP[ c->mCommand.cmd.TargetIndex ]->GetAttackPet();
						}
					}
					else
					{
						if ( c->mCommand.cmd.TargetRow == SABSR_UNDER )
						{
							if ( c->mCommand.cmd.TargetIndex < mNumberPlayer )
								target = mPlayer[ c->mCommand.cmd.TargetIndex ];
						}
						else
						{
							if ( c->mCommand.cmd.TargetIndex < mNumberPlayer )
								target = mPlayer[ c->mCommand.cmd.TargetIndex ]->GetAttackPet();
						}
					}
				}
                    break;
			}
			

			if ( !target )
			{
				return;
			}

			if ( target->IsFall() )
			{
				return;
			}

			fint16 down = 0;
			fbyte countUnder = 0;
			fbyte countUp = 0;

			for ( fuint32 i = 0 ; i < v.size() ; i++ )
			{
				down += Down( v[i] , target );

				if ( v[i]->mCommand.Row == SABSR_UNDER )
				{
					countUnder++;
				}
				else
				{
					countUp++;
				}
			}

			target->mFightData.Base.HP -= down;

			if ( target->mFightData.Base.HP < 0 )
			{
				target->mFightData.Base.HP = 1;
				target->SetFall( F_TRUE );

				for ( fuint32 i = 0 ; i < v.size() ; i++ )
				{
					v[ i ]->AddEXP( target->GetExp() );
				}
			}

			gMsgBattleResult.Num++;
			fuint16 count = 0;
			gMsgBattleResult.Data[ gMsgBattleResult.size ] = SABSCT_ATTACK;
            ++count;gMsgBattleResult.Data[ gMsgBattleResult.size + count ] = c->mCommand.Side;
            ++count;gMsgBattleResult.Data[ gMsgBattleResult.size + count ] = target->mCommand.Side;
			++count;gMsgBattleResult.Data[ gMsgBattleResult.size + count ] = (fbyte)v.size();
			++count;gMsgBattleResult.Data[ gMsgBattleResult.size + count ] = 1;
			
			for ( fuint32 i = 0 ; i < v.size() ; i++ )
			{
				++count;gMsgBattleResult.Data[ gMsgBattleResult.size + count ] = v[i]->mCommand.Row;
				++count;gMsgBattleResult.Data[ gMsgBattleResult.size + count ] = v[i]->mCommand.Index;
			}
			++count;gMsgBattleResult.Data[ gMsgBattleResult.size + count ] = c->mCommand.cmd.TargetRow;
			++count;gMsgBattleResult.Data[ gMsgBattleResult.size + count ] = c->mCommand.cmd.TargetIndex;
			++count;gMsgBattleResult.Data[ gMsgBattleResult.size + count ] = target->IsFall() ? (fbyte)SABSRT_KILL : (fbyte)SABSRT_HP;

			++count;
            MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size + count , &down , 2 );
			gMsgBattleResult.size += count + 2;
		}
		break;
	case SABSCT_DEFENCE:
		{
			gMsgBattleResult.Num++;
			
			gMsgBattleResult.Data[ gMsgBattleResult.size ] = SABSCT_DEFENCE;
			gMsgBattleResult.size++;
            gMsgBattleResult.Data[ gMsgBattleResult.size ] = c->mCommand.Side;
			gMsgBattleResult.size++;
			gMsgBattleResult.Data[ gMsgBattleResult.size ] = c->mCommand.Row;
			gMsgBattleResult.size++;
			gMsgBattleResult.Data[ gMsgBattleResult.size ] = c->mCommand.Index;
			gMsgBattleResult.size++;
		}
		break;
	case SABSCT_MAGIC:
		{
			
		}
		break;
	case SABSCT_STAY:
		{
			
		}
		break;
	case SABSCT_ESCAPE:
		{
			gMsgBattleResult.Num++;

			gMsgBattleResult.Data[ gMsgBattleResult.size ] = SABSCT_ESCAPE;
			gMsgBattleResult.size++;
			gMsgBattleResult.Data[ gMsgBattleResult.size ] = c->mCommand.Side;
			gMsgBattleResult.size++;
			gMsgBattleResult.Data[ gMsgBattleResult.size ] = c->mCommand.Row;
			gMsgBattleResult.size++;
			gMsgBattleResult.Data[ gMsgBattleResult.size ] = c->mCommand.Index;
			gMsgBattleResult.size++;

			c->SetFall( F_TRUE );
		}
		break;
	}
}


fvoid				BattleSession::AddCommand( GamePlayer* player , const CreatureFightCommand& command , const CreatureFightCommand& commandPet )
{
	for ( fint32 i = 0 ; i < MAX_BATTLE_PLAYER ; ++i )
	{
		if ( mPlayer[ i ] == player )
		{
			if ( mWaitCommand[ i ] )
			{
				mWaitCommand[ i ] = F_FALSE;
				player->mCommand.cmd = command;
				player->mCommand.Index = (fbyte)i;
				player->mCommand.Side = (fbyte)SABSS_SELF;
				player->mCommand.Row = (fbyte)SABSR_UNDER;

				GamePet* pet = player->GetAttackPet();
				if ( pet )
				{
					pet->mCommand.cmd = commandPet;
					pet->mCommand.Index = (fbyte)i;
					pet->mCommand.Side = (fbyte)SABSS_SELF;
					pet->mCommand.Row = (fbyte)SABSR_UP;
				}

				++mNumberCommand;
				break;
			}
			else
			{
				player->AddDanger( GAME_DESTRUCTIVELY_DANGER );
				return;
			}
		}
		
		if ( mPlayerPVP[ i ] == player )
		{
			if ( mWaitCommandPVP[ i ] )
			{
				mWaitCommandPVP[ i ] = F_FALSE;
				player->mCommand.cmd = command;
				player->mCommand.Index = (fbyte)i;
				player->mCommand.Side = (fbyte)SABSS_ENEMY;
				player->mCommand.Row = (fbyte)SABSR_UNDER;

				GamePet* pet = player->GetAttackPet();
				if ( pet )
				{
					pet->mCommand.cmd = commandPet;
					pet->mCommand.Index = (fbyte)i;
					pet->mCommand.Side = (fbyte)SABSS_ENEMY;
					pet->mCommand.Row = (fbyte)SABSR_UP;
				}

				++mNumberCommandPVP;
				break;
			}
			else
			{
				player->AddDanger( GAME_DESTRUCTIVELY_DANGER );
				return;
			}
		}
	}
	
	if ( mNumberCommand != mNumberPlayer || mNumberPlayerPVP != mNumberCommandPVP )
	{
		return;
	}
	
	Fight();
}


fvoid				BattleSession::Fight()
{
	gMsgBattleResult.size = 0;

	fint32 playerCount = 0;

	fint32 lastAttacker = INVALID_ID;

	mList.clear();
	mVectorAttack.clear();

	switch ( (fint32)mType )
	{
	case SABST_PVE:
		{
			for ( fint32 i = 0 ; i < mNumberPlayer ; ++i )
			{
				GamePet* pet = mPlayer[ i ]->GetAttackPet();

				if ( pet )
				{
					if ( !pet->IsFall() )
					{
						mList.push_back( pet );
					}
				}

				if ( !mPlayer[ i ]->IsFall() )
				{
					mList.push_back( mPlayer[ i ] );
				}
			}

			for ( fint32 i = 0 ; i < mNumEnemy ; ++i )
			{
				if ( mEnemyEntity[ i ].IsFall() )
				{
					continue;
				}

				ComputerRandCommand( mEnemyEntity + i );
			}

			for ( fint32 i = 0 ; i < mNumEnemy ; ++i )
			{
				if ( mEnemyEntity[ i ].IsFall() )
				{
					continue;
				}

				mList.push_back( mEnemyEntity + i );
			}

			for ( fint32 i = 0 ; i < mNumEnemyPet ; ++i )
			{
				if ( mEnemyEntityPet[ i ].IsFall() )
				{
					continue;
				}

				ComputerRandCommand( mEnemyEntityPet + i );
			}

			for ( fint32 i = 0 ; i < mNumEnemyPet ; ++i )
			{
				if ( mEnemyEntityPet[ i ].IsFall() )
				{
					continue;
				}

				mList.push_back( mEnemyEntityPet + i );
			}

		}
		break;
	case SABST_PVP:
		{
			for ( fint32 i = 0 ; i < mNumberPlayer ; ++i )
			{
				GamePet* pet = mPlayer[ i ]->GetAttackPet();

				if ( pet )
				{
					if ( !pet->IsFall() )
					{
						mList.push_back( pet );
					}
				}

				if ( !mPlayer[ i ]->IsFall() )
				{
					mList.push_back( mPlayer[ i ] );
				}
			}

			for ( fint32 i = 0 ; i < mNumberPlayerPVP ; ++i )
			{
				GamePet* pet = mPlayerPVP[ i ]->GetAttackPet();

				if ( pet )
				{
					if ( !pet->IsFall() )
					{
						mList.push_back( pet );
					}
				}

				if ( !mPlayerPVP[ i ]->IsFall() )
				{
					mList.push_back( mPlayerPVP[ i ] );
				}
			}
		}
		break;
	}

	mList.sort( mCompare );

	gMsgBattleResult.Turn = (fbyte)mTurn;
	gMsgBattleResult.Num = 0;

	GameCreatureListIter iterEnd = mList.end();
	for ( GameCreatureListIter iter = mList.begin() ; iter != iterEnd ; ++iter )
	{
		if ( lastAttacker == INVALID_ID )
		{
			if ( !(*iter)->IsFall() )
			{
				mVectorAttack.push_back( *iter );
				lastAttacker = (*iter)->mCommand.Side;
			}

			continue;
		}

		if ( lastAttacker == (*iter)->mCommand.Side && mVectorAttack[0]->mCommand.cmd.Type == SABSCT_ATTACK && (*iter)->mCommand.cmd.Type == SABSCT_ATTACK
			&& mVectorAttack[0]->mCommand.cmd.TargetIndex == (*iter)->mCommand.cmd.TargetIndex 
			&& mVectorAttack[0]->mCommand.cmd.TargetRow == (*iter)->mCommand.cmd.TargetRow )
		{
			if ( !(*iter)->IsFall() )
			{
				mVectorAttack.push_back( *iter );
			}

			continue;
		}
		else
		{
			DoResult( mVectorAttack );

			mVectorAttack.clear();

			if ( !(*iter)->IsFall() )
			{
				mVectorAttack.push_back( *iter );
				lastAttacker = (*iter)->mCommand.Side;
			}
			else
			{
				lastAttacker = INVALID_ID;
			}
		}

	}

	DoResult( mVectorAttack );

	CheckBattle();
}


fvoid				BattleSession::CheckBattle()
{
	fint32 b = 0;

	switch ( (fint32)mType )
	{
	case SABST_PVE:
		{
			for ( fint32 i = 0 ; i < mNumEnemy ; ++i )
			{
				b += mEnemyEntity[i].IsFall();
			}

			if ( b == mNumEnemy )
			{
				gMsgBattleResult.Result = SABRT_WIN;
				BattleOver();
				CloseBattle();
				return;
			}
		}
		break;
	case SABST_PVP:
		{
			for ( fint32 i = 0 ; i < mNumberPlayerPVP ; ++i )
			{
				b += mPlayerPVP[i]->IsFall();
			}

			if ( b == mNumberPlayerPVP )
			{
				gMsgBattleResult.Result = SABRT_WIN;
				BattleOver();
				CloseBattle();
				return;
			}
		}
		break;
	}


	b = 0;
	for ( fint32 i = 0 ; i < mNumberPlayer ; ++i )
	{
		b += mPlayer[i]->IsFall();
	}

	if ( b == mNumberPlayer )
	{
		gMsgBattleResult.Result = SABRT_LOSE;
		BattleOver();
		CloseBattle();
		return;
	}

	if ( mTurn != MAX_BATTLE_TURN )
	{
		gMsgBattleResult.Result = SABRT_NEXT;
		SendMsgToAll();
		NextTurn();
	}
	else
	{
		gMsgBattleResult.Result = SABRT_TIMEOUT;
		SendMsgToAll();
		CloseBattle();
	}

	
}


fvoid				BattleSession::CloseBattle()
{
	gBattleManager->ReleaseSession( this );
}


fvoid				BattleSession::SendMsgToAll()
{
	gMsgBattleResult.size += sizeof( netMsgHead ) + 3;
	for ( fint32 i = 0 ; i < mNumberPlayer ; ++i )
	{
		mPlayer[ i ]->PackMsg( &gMsgBattleResult );
		mPlayer[ i ]->SendMsg();
	}

	for ( fint32 i = 0 ; i < mNumberPlayerPVP ; ++i )
	{
		if ( gMsgBattleResult.Result == SABRT_WIN )
		{
			 gMsgBattleResult.Result = SABRT_LOSE;
		}
		else if ( gMsgBattleResult.Result == SABRT_LOSE )
		{
			gMsgBattleResult.Result = SABRT_WIN;
		}

		mPlayerPVP[ i ]->PackMsg( &gMsgBattleResult );
		mPlayerPVP[ i ]->SendMsg();
	}
}


fvoid				BattleSession::NextTurn()
{
	++mTurn;

	mNumberCommand = 0;
	mNumberCommandPVP = 0;

	for ( fint32 i = 0 ; i < MAX_BATTLE_PLAYER ; ++i )
	{
		mWaitCommand[ i ] = F_TRUE;
		mWaitCommandPVP[ i ] = F_TRUE;
	}
}


fvoid				BattleSession::BattleOver()
{
	switch ( gMsgBattleResult.Result )
	{
	case SABRT_WIN:
		{
			switch ( (fint32)mType )
			{
			case SABST_PVP:
				{
					SendMsgToAll();
				}
				break;
			case SABST_PVE:
				{
					gMsgBattleResult.Data[ gMsgBattleResult.size ] = mNumberPlayer;
					++gMsgBattleResult.size;

					for ( fint32 i = 0 ; i < mNumberPlayer ; ++i )
					{
						mPlayer[i]->CheckLevelUp();

						gMsgBattleResult.Data[ gMsgBattleResult.size ] = mPlayer[i]->mData.Level;
						++gMsgBattleResult.size;
						MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size , &mPlayer[i]->mData.LevelUpPoint , 2 );
						gMsgBattleResult.size += 2;
						MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size , &mPlayer[i]->mTempExp , 4 );
						gMsgBattleResult.size += 4;
						MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size , &mPlayer[i]->mData.EXP , 4 );
						gMsgBattleResult.size += 4;
						MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size , &mPlayer[i]->mData.NEXTEXP , 4 );
						gMsgBattleResult.size += 4;
					}

					fint32 size = gMsgBattleResult.size;

					for ( fint32 i = 0 ; i < mNumberPlayer ; ++i )
					{
						gMsgBattleResult.Data[ size ] = 0;
						++gMsgBattleResult.size;

						GamePetMap& petMap = mPlayer[i]->GetPetMap();
						GamePetMapIter iterEnd = petMap.end();
						for ( GamePetMapIter iter = petMap.begin() ; iter != iterEnd ; ++iter )
						{
							if ( iter->second.mTempExp )
							{
								gMsgBattleResult.Data[ size ]++;

								iter->second.CheckLevelUp();

								MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size , &iter->second.mData.ID , 4 );
								gMsgBattleResult.size += 4;

								gMsgBattleResult.Data[ gMsgBattleResult.size ] = iter->second.mData.Level;
								++gMsgBattleResult.size;
								gMsgBattleResult.Data[ gMsgBattleResult.size ] = iter->second.mFightData.Base.Charm;
								++gMsgBattleResult.size;
								MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size , &iter->second.mData.Vitality , 2 );
								gMsgBattleResult.size += 2;
								MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size , &iter->second.mData.Strength , 2 );
								gMsgBattleResult.size += 2;
								MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size , &iter->second.mData.Toughness , 2 );
								gMsgBattleResult.size += 2;
								MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size , &iter->second.mData.Dexterity , 2 );
								gMsgBattleResult.size += 2;

								MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size , &iter->second.mTempExp , 4 );
								gMsgBattleResult.size += 4;
								MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size , &iter->second.mData.EXP , 4 );
								gMsgBattleResult.size += 4;
								MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size , &iter->second.mData.NEXTEXP , 4 );
								gMsgBattleResult.size += 4;

							}
						}

						fint32 size1 = gMsgBattleResult.size;
						gMsgBattleResult.Data[ size1 ] = 0;
						++gMsgBattleResult.size;

						for ( fint32 j = 0 ; j < mNumEnemy ; j++ )
						{
							GameEnemy* enemy = mEnemyEntity[ j ].GetEnemy();

							for ( fint32 k = 0 ; k < 10 ; k++ )
							{
								if ( enemy->intValue[ GEIV_ITEMPRO0 + k ] )
								{
									fint32 index = mPlayer[ i ]->GetItemBag().AddItem( enemy->intValue[ GEIV_ITEM0 + k ] );

									if ( index != INVALID_ID )
									{
										gMsgBattleResult.Data[ size1 ]++;

										Item& item = mPlayer[ i ]->GetItemBag().GetItem( index );
										ItemData* data = item.GetData();

										ItemInfo info;
										info.i = index;
										info.Item = 1;
										info.Type = data->Type;
										info.ID = data->ID;
										info.Image = data->Img;
										info.Sell = data->Sell;
										info.Num = item.GetNum();

										info.Px = data->Px;
										info.Py = data->Py;

										MEMCPY( gMsgBattleResult.Data + gMsgBattleResult.size , &info , sizeof( ItemInfo ) );
										gMsgBattleResult.size += sizeof( ItemInfo );
									}

								}
							}
						}

						gMsgBattleResult.size += sizeof( netMsgHead ) + 3;

						mPlayer[ i ]->PackMsg( &gMsgBattleResult );
						mPlayer[ i ]->SendMsg();
					}

				}
				break;
			}


		}
		break;
	case SABRT_LOSE:
		{
			SendMsgToAll();
		}
		break;

	}

}


